package com.lxl.hadoop.flowtopn;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author lixilin
 * @Date 2021/12/5 19:48
 * @Description
 */
public class TopNFlowBean implements WritableComparable<TopNFlowBean> {
    private long upFlow;
    private long downFlow;
    private long sumFlow;

    /**
     * 空参构造,后续反射用
     */
    public TopNFlowBean() {
        super();
    }

    public TopNFlowBean(long upFlow, long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = downFlow + upFlow;
    }

    /**
     * 序列化方法
     *
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);
    }

    /**
     * 反序列化方法
     *
     * @param in
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        //必须要求和序列化方法的字段顺序一致
        upFlow = in.readLong();
        downFlow = in.readLong();
        sumFlow = in.readLong();
    }

    /**
     * 倒序
     *
     * @param bean
     * @return
     */
    @Override
    public int compareTo(TopNFlowBean bean) {
        long sumFlow1 = this.getSumFlow();
        long sumFlow2 = bean.getSumFlow();
        if (sumFlow1 == sumFlow2) {
            return 0;
        }
        if (sumFlow1 < sumFlow2) {
            return 1;
        }
        return -1;
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public void set(long upFlow, long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = downFlow + upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public String toString() {
        return upFlow +
                "\t" + downFlow +
                "\t" + sumFlow;
    }

   /* @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TopNFlowBean bean = (TopNFlowBean) o;
        return upFlow == bean.upFlow &&
                downFlow == bean.downFlow &&
                sumFlow == bean.sumFlow;
    }

    @Override
    public int hashCode() {
        return (upFlow+"").hashCode()+ (downFlow+"").hashCode()+(sumFlow+"").hashCode();
    }*/
}
